using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using Deepcode.Flux.Core.UI.Controls.Base;

namespace Deepcode.Flux.Core.UI.Controls
{
	/// <summary>
	/// ContentLinkValidator is a special validation control that
	/// ensures a contentlink contains a valid selection. It also exposes
	/// a client side validation routine.
	/// </summary>
	public class ContentLinkValidator : Deepcode.Flux.Core.UI.Controls.Base.BaseValidator
	{
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);
			this.LoadClientScript("ContentLink/clv.js", "Deepcode.Flux.Core.UI.Controls.ContentLinkValidator");
		}

		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender (writer);
			if( RenderUplevel )
			{
				writer.AddAttribute("evaluationfunction", "CL_Validate");
			}
		}

		protected override bool EvaluateIsValid()
		{
			try
			{
				// Take the text from the control
				string s = this.GetControlValidationValue( ControlToValidate );
				// Parse it to an int
				int iv = Int32.Parse(s);
				if( iv == -1 ) return false;
				return true;
			}
			catch
			{
				return false;
			}
		}
	}
}